package com.cans.proxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 *
 * @author cans
 * @date 2018/3/4
 */
public class ProxyFactory {

    /**
     * 目标对象
     */
    private Object target;

    public ProxyFactory(Object target){
        this.target = target;
    }

    /**
     * 给目标对象生成代理
     * @return
     */
    public Object getProxyInstance(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (Object proxy, Method method, Object[] args) -> {
                    System.out.println("开始事务2");
                    //执行目标对象方法
                    Object returnValue = method.invoke(target, args);
                    System.out.println("提交事务2");
                    return returnValue;
                });
    }
}
